import {defHttp} from '/@/utils/http/axios';
import { Modal } from "ant-design-vue";
import { useMessage } from "/@/hooks/web/useMessage";
const { createMessage: $message } = useMessage();
import { repeal } from "/@/views/shop/orders/orders.api";

enum Api {
  //订单列表 - 列表
  list = '/business/datagrid',
  businessDelete = '/business/delete',
  exportXlsUrl = '/business/exportXls',

  //订单信息 - 操作
  businessAdd  = '/business/save',
  businessById = '/business/queryById',
  businessStat = '/business/ajaxBusinessStat',
  businessUpdate = '/business/update',

  //采购商 - 操作
  businessCustomerById = '/businessCustomer/queryById',
  businessCustomerSave = '/businessCustomer/save',
  businessCustomerDelete = '/businessCustomer/delete',
  businessCustomerStatus = '/businessCustomer/updateStatus',
  asyncZtreeNode = '/customer/asyncZtreeNode',

  //收支项目 - 操作
  accountById   = '/account/queryById',
  accountSave   = '/account/save',
  accountDelete = '/account/delete',
  accountStat   = '/account/ajaxAccountStat',
  accountRecall = '/account/ajaxAccountRecall',
  coopSelectUrl = '/cooperator/regeionSelect',
  accountPrint  = '/account/accountPrint',

  //订单列表 - 导入导出
  exportXls = '/business/exportXls',
  importXls = '/business/importExcel',

  ajaxAutocomplete = '/cmsScheduleController/ajaxAutocomplete'
}

//TODO -----------订单列表 ---------------------------------------------
/**
 * 订单 - 列表
 * @param params
 */
export const list = (params) => defHttp.get({url: Api.list, params}, {joinParamsToUrl: true});

/**
 * 订单 - 删除
 */
export const businessDelete = (params, handleSuccess) => {
  Modal.confirm({
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({ url: Api.businessDelete, params }, {
        joinParamsToUrl: true,
        isTransformResponse: false
      }).then((res) => {
        if (res.success) {
          $message.success(res.message)
          handleSuccess()
        } else {
          $message.error(res.message)
        }
      });
    },
  });
}

//TODO -----------订单新增 ---------------------------------------------
/**
 * 订单 - 新增
 * @param params
 */
export const add = (params) => {
  return defHttp.post({url: Api.businessAdd, params}, {isTransformResponse:false});
}

/**
 * 订单 - 编辑
 * @param params
 */
export const businessUpdate = (params) => {
  return defHttp.post({url: Api.businessUpdate, params}, {isTransformResponse:false});
}

//TODO -----------订单管理 --状态-------------------------------------------
/**
 * 订单 - 状态
 * @param params
 */
export const businessStat = (params) => defHttp.get({url: Api.businessStat, params}, {isTransformResponse:false});


/**
 * 订单信息 - 所有
 * @param params
 */
export const businessById = (params) => defHttp.get({url: Api.businessById, params}, {isTransformResponse:false});

//TODO -----------订单管理 --供应商-------------------------------------------
/**
 * 供应商 - 信息
 * @param params
 */
export const businessCustomerById = (params) => defHttp.get({url: Api.businessCustomerById, params}, {isTransformResponse:false});

/**
 * 供应商 - 添加｜编辑
 * @param params
 */
export const businessCustomerSave = (params) => {
  return defHttp.post({url: Api.businessCustomerSave, params});
}

/**
 * 供应商 - 删除
 * @param params
 */
export const businessCustomerDel = (params, handleSuccess) => {
  Modal.confirm({
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({ url: Api.businessCustomerDelete, data: params }, { joinParamsToUrl: true }).then(() => {
        handleProductOrderRepeal(params);
        handleSuccess();
      });
      // console.info("businessCustomerDel@@", params)
      // handleProductOrderRepeal(params);
    },
  });
};

const handleProductOrderRepeal = (params) => {
  try {
    if (!params.orderSn) {
      return false;
    }
    repeal({orderSn: params.orderSn});
  } catch (e) {
    console.log(e);
  } 
};

/**
 * 供应商 - 审核
 * @param params
 */
 export const businessCustomerStatus = (params) => {
  return defHttp.put({url: Api.businessCustomerStatus, params}, { joinParamsToUrl: true, isTransformResponse:false });
}

//TODO -----------收支项目 ---------------------------------------------
/**
 * 收支项目 - 信息
 * @param params
 */
export const accountById = (params) => defHttp.get({url: Api.accountById, params});

/**
 * 收支项目 - 添加｜编辑
 * @param params
 */
export const accountSave = (params) => {
  return defHttp.post({url: Api.accountSave, params}, {isTransformResponse:false});
}

/**
 * 供应商 - 删除
 * @param params
 */
export const accountDelete = (params, handleSuccess) => {
  Modal.confirm({
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({ url: Api.accountDelete, data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess();
      });
    },
  });
};

/**
 * 收支项目 - 提交
 * @param params
 */
export const accountStat = (params) => {
  return defHttp.post({url: Api.accountStat, params}, { joinParamsToUrl: true, isTransformResponse:false });
}

/**
 * 收支项目 - 招回
 * @param params
 */
export const accountRecall = (params) => {
  return defHttp.post({url: Api.accountRecall, params},{ joinParamsToUrl:true, isTransformResponse:false });
}

/**
 * 收支项目 - 支出树型
 * @param params
 */
export const coopSelect = () => defHttp.get({url: Api.coopSelectUrl}, {isTransformResponse:false});

/**
 * 收支项目 - 打印
 * @param params
 */
export const accountPrint = (params) => defHttp.get({url: Api.accountPrint, params});


/**
 * 订单 - 订单添加 - 产品线路
 * @param params
 */
export const ajaxAutocomplete = (params) => defHttp.get({url: Api.ajaxAutocomplete, params}, {isTransformResponse:false});